package com.cn.jdia.dc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cn.jdia.dc.context.SpringContextUtils;
import com.cn.jdia.dc.model.Dbconfig;
import com.cn.jdia.dc.model.Dbconnect;
import com.cn.jdia.dc.service.IDbconfigService;
import com.cn.jdia.dc.service.IDbconnectService;

@Controller
public class MonitorController {

	@Resource
	private IDbconfigService dbservice;

	@Resource
	private IDbconnectService connectService;

	@RequestMapping(value = "/dbmonitor")
	@ResponseBody
	public ModelAndView dbmonitor() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("dbmonitor");

		dbservice = (IDbconfigService) SpringContextUtils.getContext().getBean("dbconfigService");
		List<Dbconfig> allDbConfig = dbservice.queryAll();

		List<Map<String, String>> list = new ArrayList<>();
		Map<String, String> map = null;
		String url = null;
		int ret = -1;

		for (Dbconfig df : allDbConfig) {
			map = new HashMap<>();

			map.put("sname", df.getServername());
			map.put("ip", df.getIp());
			map.put("port", df.getPort());
			map.put("defaultdb", df.getDefaultdb());
			map.put("user", df.getUser());
			map.put("type", df.getDbtype());

			if ("right".equals(df.getChecked())) {
				map.put("status", "0");
			} else {
				map.put("status", "-1");
			}

			list.add(map);
		}
		mav.addObject("list", list);

		return mav;
	}

	@RequestMapping(value = "/cnmonitor")
	@ResponseBody
	public ModelAndView cnmonitor() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("cnmonitor");

		List<Map<String, Object>> clist = new ArrayList<>();
		Map<String, Object> cmap;
		List<Dbconnect> allConnect = connectService.getAllConnect();
		for (Dbconnect con : allConnect) {
			cmap = new HashMap<>();
			cmap.put("sname", con.getServername());
			cmap.put("ip", con.getIp());
			cmap.put("active", con.getActiveconnect());
			cmap.put("max", con.getMaxconnect());
			cmap.put("warn", con.getWarn());

			clist.add(cmap);
		}

		mav.addObject("clist", clist);

		return mav;
	}
}
